﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class QLLopHoc : Form
    {
        public QLLopHoc()
        {
            InitializeComponent();
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            
            refreshData();
        }

        private void refreshData()
        {
            this.view_LopHocTableAdapter.Connection.ConnectionString = 
                PublicLib.getStrConn();
            this.view_LopHocTableAdapter.FillByFlag(this.qLTH.view_LopHoc, PConst.FLAG_NONE);

            reloadData();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 20;
            dg_DanhSach.Height = this.Height - pn_TimKiem.Height - 50;

            pn_TimKiem.Top = pn_Button.Top = dg_DanhSach.Bottom + 5;
            pn_Button.Left = dg_DanhSach.Right - pn_Button.Width;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            reloadData();
        }

        private void reloadData()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[TenDayDuGV.Name].Value =
                    dg_DanhSach.Rows[i].Cells[HoDem.Name].Value.ToString() + " " +
                    dg_DanhSach.Rows[i].Cells[TenGV.Name].Value.ToString();
            }
        }

        private void dg_DanhSach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == TenDayDuGV.Index)
            {
                switch (TenDayDuGV.HeaderCell.SortGlyphDirection)
                {
                    case SortOrder.None:
                    case SortOrder.Descending:
                        dg_DanhSach.Sort(TenGV, ListSortDirection.Ascending);
                        TenDayDuGV.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                        break;

                    case SortOrder.Ascending:
                        dg_DanhSach.Sort(TenGV, ListSortDirection.Descending);
                        TenDayDuGV.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                        break;
                }
            }
            else
                TenDayDuGV.HeaderCell.SortGlyphDirection = SortOrder.None;
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditLopHoc frm_ThemLop = new AddEditLopHoc();
            frm_ThemLop.Show();
            frm_ThemLop.Disposed += new EventHandler(frm_ThemLop_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            AddEditLopHoc frm_EditLop = new AddEditLopHoc();
            frm_EditLop.Show();
            frm_EditLop.setEditInfo(int.Parse(dg_DanhSach.SelectedRows[0].Cells[idDataGridViewTextBoxColumn.Name].Value.ToString()));
            frm_EditLop.Disposed += new EventHandler(frm_ThemLop_Disposed);
        }

        private void frm_ThemLop_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bt_Sua_Click(bt_Sua, new EventArgs());
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;

            deleteGV();
            refreshData();
        }

        private void deleteGV()
        {
            String idLopHoc = dg_DanhSach.SelectedRows[0].Cells[idDataGridViewTextBoxColumn.Name].Value.ToString();
            MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter adap = new tbl_LopHocTableAdapter();
            adap.Fill(qLTH.tbl_LopHoc);

            DataRow gv = qLTH.tbl_LopHoc.Select(qLTH.tbl_LopHoc.idColumn.ColumnName + "=" + idLopHoc)[0];
            gv[qLTH.tbl_LopHoc.flagColumn] = PConst.FLAG_DELETE;
            adap.Update(gv);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteGV();
        }
    }
}
